<?php 

/**
 * 日志处理类
 * @author lihuafeng
 * @date 20161223
 */
 namespace Org\Monolog;
 require './ThinkPHP/Library/Org/Monolog/Formatter/LineFormatter.class.php';
 Class MonologClient {

    //all logs will be saved to "all"
    const DEFAULT_FILENAME = 'all';

    private $loggers = array();

    private $dateFormat = "Y-m-d H:i:s";
    private $outputFormat = "[%level_name%] [%datetime%] %message% \n";

    /**
     * print the debug level message in the corresponding log file
     *
     * @param string $content log message
     * @param string $filename file name you specified in your monolog config file
     * @return Boolean TRUE if log file configured, otherwise return FALSE
     */
    public function debug($content, $filename=self::DEFAULT_FILENAME) {
        if ($filename !== self::DEFAULT_FILENAME) {
            $this->do_log(self::DEFAULT_FILENAME, $content, Logger::DEBUG);
        }
        return $this->do_log($filename, $content, Logger::DEBUG);
    }

    public function info($content, $filename=self::DEFAULT_FILENAME) {
        if ($filename !== self::DEFAULT_FILENAME) {
            $this->do_log(self::DEFAULT_FILENAME, $content, Logger::INFO);
        }
        return $this->do_log($filename, $content, Logger::INFO);
    }

    public function warning($content, $filename=self::DEFAULT_FILENAME) {
        if ($filename !== self::DEFAULT_FILENAME) {
            $this->do_log(self::DEFAULT_FILENAME, $content, Logger::WARNING);
        }
        return $this->do_log($filename, $content, Logger::WARNING);
    }

    public function error($content, $filename=self::DEFAULT_FILENAME) {
        if ($filename !== self::DEFAULT_FILENAME) {
            $this->do_log(self::DEFAULT_FILENAME, $content, Logger::ERROR);
        }
        return $this->do_log($filename, $content, Logger::ERROR);
    }

    public function critical($content, $filename=self::DEFAULT_FILENAME) {
        if ($filename !== self::DEFAULT_FILENAME) {
            $this->do_log(self::DEFAULT_FILENAME, $content, Logger::CRITICAL);
        }
        return $this->do_log($filename, $content, Logger::CRITICAL);
    }

    private function do_log($filename, $content, $level) {
        foreach ($this->loggers as $log_item) {
            if ($log_item['filename'] == $filename) {
                $log_level = $log_item['loglevel'];
                if ($log_level <= $level) {
                    $log_item['logger']->addRecord($level, $content);
                }
                return TRUE;
            }
        }
        return FALSE;
    }

    public function __construct() {
        $configs = self::get_config();
        if ($configs) {
            $i = 0;
            //create formatter
            $formatter = new LineFormatter($this->outputFormat, $this->dateFormat);
            foreach ($configs as $log_config) {
                //create handler
                $handler = new MyHandler($log_config['logpath'], $log_config['filename'], Logger::DEBUG);
                $handler->setFormatter($formatter);

                // create a log channel
                $i++;
                $logger = new Logger('monolog_'.$i);
                $logger->pushHandler($handler);

                $log_item = array('filename' => $log_config['filename'],
                        'loglevel' => $log_config['loglevel'],
                        'logger' => $logger);
                array_push($this->loggers, $log_item);
            }
        }
    }

    private static function get_config() {
        if (C('Monolog')) {
            return C('Monolog');
        } else {
            return FALSE;
        }
    }

}

